home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_gnats.idb / usr / freeware / bin / edit-pr.z / edit-pr
Text File  |  1999-04-16  |  7KB  |  271 lines

  1. #!/bin/sh
  2. # Program to edit problem reports for GNATS.
  3. # Copyright (C) 1993 Free Software Foundation, Inc.
  4. # Contributed by Jeffrey Osier (jeffrey@cygnus.com).
  5. #
  6. # This file is part of GNU GNATS.
  7. #
  8. # GNU GNATS is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2, or (at your option)
  11. # any later version.
  12. #
  13. # GNU GNATS is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with GNU GNATS; see the file COPYING.  If not, write to
  20. # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. DATE=`date`
  23. GNATS_ROOT=/usr/freeware/lib/gnats/gnats-db
  24. LIBDIR=/usr/freeware/lib
  25. PATH=$PATH:$GNATS_ROOT/gnats-bin
  26. PR_EDIT=$LIBDIR/gnats/pr-edit
  27. PR_ADDR=$LIBDIR/gnats/pr-addr
  28. locked=
  29. version=3.2
  30.  
  31. usage="Usage: $0 [-hV] [--version] [--help] PR"
  32.  
  33. # Newer config information?
  34. [ -f ${GNATS_ROOT}/gnats-adm/config ] && . ${GNATS_ROOT}/gnats-adm/config
  35.  
  36. # Host-specific; must come after config file.
  37. MAIL_AGENT="/usr/lib/sendmail -oi -t"
  38.  
  39. # check to see if there is a $EDITOR; if not, use vi
  40. [ -z "$VISUAL" ] &&
  41.   if [ -z "$EDITOR" ]; then
  42.     VISUAL=vi
  43.   else
  44.     VISUAL="$EDITOR"
  45.   fi
  46.  
  47. # parse command line.
  48. # for the non-flag argument, assume it's correct.  if it's a full
  49. # id number, use that; if not, find the full id.
  50. # only continue if $full_id is an actual file.
  51.  
  52. if [ $# -eq 0 ]; then
  53.   echo $usage ; exit 1
  54. fi
  55. case "$1" in
  56.   -V|-v|--version|--ve*)
  57.     echo "$version"; exit 0
  58.     ;;
  59.   -h|--help*)
  60.     echo "$usage"; exit 0
  61.     ;;
  62.   -*)
  63.     echo "$usage"; exit 1
  64.     ;;
  65.   *)
  66.     if [ "`echo $1 | grep /`" != "" ]; then
  67.       full_id=$1
  68.     else
  69.       full_id=`grep "/$1:" $GNATS_ROOT/gnats-adm/index | awk -F: '{print $1}' -`
  70.     fi
  71.     ;;
  72. esac
  73.  
  74. trap 'rm -f /tmp/u$$ $new ; exit 0' 0
  75. trap 'if [ "$locked" != "" ]; then \
  76.         $PR_EDIT --unlock $full_id ; \
  77.     locked= ; \
  78.       fi ; \
  79.       rm -f /tmp/u$$ $new ; exit 1' 1 2 3 13 15
  80.  
  81. # check $full_id
  82. pr=$GNATS_ROOT/$full_id    # pr = full path of editee
  83.  
  84. if [ "$full_id" = "" ]; then
  85.   echo "edit-pr: file not found" ; echo $usage ; exit 1
  86. else
  87.   if [ ! -f $pr -o ! -r $pr ]; then
  88.     echo "edit-pr: cannot read PR $full_id"
  89.     echo $usage ; exit 1
  90.   fi
  91. fi
  92.  
  93. # find a user name
  94. if [ "$USER" != "" ]; then
  95.   me=$USER
  96. else
  97.   if [ "$LOGNAME" != "" ]; then
  98.     me=$LOGNAME
  99.   else
  100.     echo "edit-pr: no user name found---set LOGNAME." ; exit 1
  101.   fi
  102. fi
  103.  
  104. if [ -z "$HOSTNAME" ]; then
  105.   if [ -f /bin/hostname ] ; then HOSTNAME=`/bin/hostname`
  106.   elif [ -f /usr/bin/hostname ] ; then HOSTNAME=`/usr/bin/hostname`
  107.   # Solaris et al.
  108.   elif [ -f /usr/ucb/hostname ] ; then HOSTNAME=`/usr/ucb/hostname`
  109.   # Irix
  110.   elif [ -f /usr/bsd/hostname ] ; then HOSTNAME=`/usr/bsd/hostname`
  111.   fi
  112. fi
  113.  
  114. if [ -n "$HOSTNAME" ]; then
  115.   full_me="$me@$HOSTNAME"
  116. else
  117.   full_me="$me"
  118. fi
  119.  
  120. # now we have a valid $full_id.. use its full path
  121. # idea is to have $pr=original pr (unchanged), $new=temp version of $pr
  122.  
  123. # new = temp file to use for editing
  124. new="/tmp/ep$$"
  125. # make a copy of pr in new
  126. cp $pr $new
  127.  
  128. # lock the pr
  129. $PR_EDIT --lock $full_me $full_id 2> /tmp/u$$
  130. locked=t
  131.  
  132. if [ -s /tmp/u$$ ]; then
  133.   if [ "`grep exists /tmp/u$$`" = "" ]; then
  134.     echo "edit-pr: PR $full_id is locked by `sed 's/.*by //g' /tmp/u$$`"
  135.   else
  136.     echo "edit-pr: GNATS is presently locked, try again in a moment"
  137.   fi
  138.   rm -f /tmp/u$$
  139.   exit 1
  140. fi
  141.  
  142. # here's where we actually call the editor.
  143. cp $new $new.old
  144. $VISUAL $new
  145. if cmp -s $new.old $new ; then
  146.   echo "edit-pr: PR not changed"
  147.   $PR_EDIT --unlock $full_id
  148.   exit 0
  149. fi
  150. rm -f $new.old
  151.  
  152. # error-check output by calling pr-edit --check; if mistakes exist,
  153. # call $VISUAL or exit
  154. checking=t
  155. while [ "$checking" != "" ]; do
  156.   errors="`$PR_EDIT --check < $new`"
  157.   if [ "$errors" != "" ]; then
  158.     echo "Hit \`return\` to fix the following errors, or type \'quit\' to quit:"
  159.     echo "$errors"
  160.     read fixme
  161.     case "$fixme" in
  162.       q* | Q*) 
  163.         echo "PR $full_id not updated: changed file is in $new"
  164.     $PR_EDIT --unlock $full_id
  165.         exit 0
  166.         ;;
  167.     esac
  168.     $VISUAL $new
  169.   else
  170.     checking=
  171.   fi
  172. done
  173.  
  174. # now that we have a clean new PR
  175. # check for changes in Responsible or State
  176. # add audit trail
  177. # mail changes to relevant parties
  178.  
  179. old_state="`sed -n '/^>State:/{s,^>[-a-zA-Z]*: *,,;p;q;}' $pr`"
  180. new_state="`sed -n '/^>State:/{s,^>[-a-zA-Z]*: *,,;p;q;}' $new`"
  181. old_resp="`sed -n '/^>Responsible:/{s,^>[-a-zA-Z]*: *,,;s, *(.*,,g;p;q;}' $pr`"
  182. new_resp="`sed -n '/^>Responsible:/{s,^>[-a-zA-Z]*: *,,;s, *(.*,,g;p;q;}' $new`"
  183. old_synopsis="`sed -n '/^>Synopsis:/{s,^>[-a-zA-Z]*: *,,;p;q;}' $pr`"
  184. new_synopsis="`sed -n '/^>Synopsis:/{s,^>[-a-zA-Z]*: *,,;p;q;}' $new`"
  185.  
  186. # If you can read this, you may have a future in sed(1) programming.
  187. reply_to="`sed -n \
  188. -e '/^$/{g;s/    / /g;s/\n/ /g;s/^.*: *//;s/ *(.*) *//;s/.*<//;s/>.*//;p;q;}' \
  189. -e '/^Reply-To:/h' \
  190. -e '/^Reply-To:/,/^[^     ]/{s/^[^     ].*//;H;}' \
  191. -e '/^Reply-To:/,$b' \
  192. -e '/^From:/h' \
  193. -e '/^From:/,/^[^     ]/{s/^[^     ].*//;H;}' \
  194. $pr`"
  195.  
  196. change_msg=/tmp/ed_pr_ch$$
  197.  
  198. # the following could stand to be cleaned up...
  199. if [ "$old_state" != "$new_state" ]; then
  200.   state_change=yes
  201. fi
  202. if [ "$old_resp" != "$new_resp" ]; then
  203.   resp_change=yes
  204. fi
  205.  
  206. if [ ! -z "$state_change" ] || [ ! -z "$resp_change" ]; then
  207.   # we've got a change
  208.   mail_to="$me"
  209.   if [ ! -z "$state_change" ]; then
  210.     echo State-Changed-From-To: "$old_state"-"$new_state" >> $change_msg
  211.     echo State-Changed-By: $me >> $change_msg
  212.     echo State-Changed-When: $DATE >> $change_msg
  213.     echo "State-Changed-Why: " >> $change_msg
  214.     echo 'Why did the state change? (Ctrl-D to end)'
  215.     cat >> $change_msg
  216.     to_old=1
  217.     to_subm=1
  218.   fi
  219.   if [ ! -z "$state_change" ] && [ ! -z "$resp_change" ]; then
  220.     echo "" >> $change_msg
  221.     echo "" >> $change_msg
  222.   fi
  223.   if [ ! -z "$resp_change" ]; then
  224.     echo Responsible-Changed-From-To: "$old_resp"'->'"$new_resp" >> $change_msg
  225.     echo Responsible-Changed-By: $me >> $change_msg
  226.     echo Responsible-Changed-When: $DATE >> $change_msg
  227.     echo "Responsible-Changed-Why: " >> $change_msg
  228.     echo 'Why did the responsible person change? (Ctrl-D to end)'
  229.     cat >> $change_msg
  230.     to_old=1
  231.     to_new=1
  232.   fi
  233.  
  234.   if [ -z "$to_subm" ]; then mail_to="${reply_to}, ${mail_to}" ; fi
  235.   if [ -z "$to_old" ] ; then mail_to="${mail_to}, `$PR_ADDR ${old_resp}`" ; fi
  236.   if [ -z "$to_new" ] ; then mail_to="${mail_to}, `$PR_ADDR ${new_resp}`" ; fi
  237.  
  238.   # We have to quote the end of each line in the change_msg, so that sed
  239.   # won't try to use it as a new command.
  240.   sed -e "/^>Unformatted:/i\\
  241. `sed -e 's/$/ \\\\/g' $change_msg`
  242. " $new > $new.tmp
  243.   mv -f $new.tmp $new
  244.  
  245.   $MAIL_AGENT << __EOF__
  246. To: $mail_to
  247. From: $me
  248. Subject: Changed information for PR $full_id
  249.  
  250. `if [ "$old_synopsis" != "$new_synopsis" ]; then
  251.     echo Old Synopsis: "$old_synopsis"
  252.     echo New Synopsis: "$new_synopsis"
  253. else
  254.     echo Synopsis: "$old_synopsis"
  255. fi`
  256.  
  257. `cat $change_msg`
  258. __EOF__
  259. fi
  260.  
  261. echo "edit-pr: filing $full_id back into the database"
  262.  
  263. # call PR_EDIT on the new file and clean up
  264. $PR_EDIT < $new
  265. $PR_EDIT --unlock $full_id
  266.  
  267. [ -f "$new" ] && rm -f $new
  268. [ -f "$change_msg" ] && rm -f $change_msg
  269.  
  270. exit 0
  271.